const express=require('express');
const router=express.Router();
//前端分页
router.post('/getstudent',(req,res)=>{
    res.json({
        data:[
            {id:1,name:'aaa','sex':'男',age:12},
            {id:2,name:'bbb','sex':'女',age:12},
            {id:3,name:'ccc','sex':'男',age:12},
            {id:4,name:'ddd','sex':'女',age:12},
            {id:5,name:'eee','sex':'女',age:12},
            {id:6,name:'fff','sex':'男',age:12},
            {id:7,name:'ggg','sex':'女',age:12},
            {id:8,name:'hhh','sex':'男',age:12},
            {id:9,name:'iii','sex':'女',age:12},
            {id:10,name:'jjj','sex':'男',age:12},
            {id:11,name:'kkk','sex':'女',age:12},

        ],
        msg:'success'
    })
})
//后台分页
let data=[
    {id:1,name:'aaa','sex':'男',age:12},
    {id:2,name:'bbb','sex':'女',age:12},
    {id:3,name:'ccc','sex':'男',age:12},
    {id:4,name:'ddd','sex':'女',age:12},
    {id:5,name:'eee','sex':'女',age:12},
    {id:6,name:'fff','sex':'男',age:12},
    {id:7,name:'ggg','sex':'女',age:12},
    {id:8,name:'hhh','sex':'男',age:12},
    {id:9,name:'iii','sex':'女',age:12},
    {id:10,name:'jjj','sex':'男',age:12},
    {id:11,name:'kkk','sex':'女',age:12},
]
router.post('/getstudent2',(req,res)=>{
    const {page,count}=req.body;
    res.json({
        msg:"success",
        data:data.slice((page-1)*count,page*count),
        _total:data.length
    }) 
})
//增加
router.post('/addstudent',(req,res)=>{
    const {name,sex,age}=req.body;
    data.push({id:data.length+1,name,sex,age});
    res.json({
        msg:"success"
    }) 
})
//删除
router.post('/delstudent',(req,res)=>{
    const {id}=req.body;
    let index=0;
    data.forEach((v,i)=>{
        if(id==v.id){
            index=i;
        }
    })
    data.splice(index,1);
    res.json({
        msg:"success"
    }) 
})
module.exports=router;
